<div class="layuimini-main">
	<div class="layui-form layuimini-form" lay-filter="form-data">
		<div class="layui-form-item">
			<label class="layui-form-label required">模板名称</label>
			<div class="layui-input-block">
				<input type="text" name="name" lay-verify="required" lay-reqtext="模板名称不能为空" placeholder="请输入模板名称" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">服务地址</label>
			<div class="layui-input-block">
				<input type="text" name="serviceUrl" lay-verify="required" lay-reqtext="服务地址不能为空" placeholder="如：127.0.0.1:11800" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">技术类型</label>
			<div class="layui-input-block">
				<select name="techType" lay-filter="techType" lay-verify="required" lay-reqtext="技术类型不能为空">
					<option value="">请选择</option>
					<option value="1">Java</option>
					<!--
					<option value="2">Python</option>
					<option value="3">Node</option>
					-->
				</select>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label required">Agent镜像来源</label>
			<div class="layui-input-block">
				<select name="agentImageSource" lay-filter="agentImageSource" lay-verify="required" lay-reqtext="Agent镜像来源不能为空">
					<option value="">请选择</option>
					<option value="1">版本号</option>
					<option value="2">自定义</option>
				</select>
			</div>
		</div>
		<div class="layui-form-item" style="display:none" id="agentVersionDiv">
			<label class="layui-form-label required">Agent版本</label>
			<div class="layui-input-inline">
				<select name="agentVersion" lay-filter="agentVersion">
					<option value="">请选择</option>
					<option value="8.14.0">v8.14.0</option>
					<option value="8.13.0">v8.13.0</option>
					<option value="8.12.0">v8.12.0</option>
					<option value="8.11.0">v8.11.0</option>
					<option value="8.10.0">v8.10.0</option>
				</select>
			</div>
			<div class="layui-form-mid layui-word-aux">会自动下载依赖包，请确保能够访问外网</div>
		</div>
		<div class="layui-form-item required" style="display:none" id="agentImageDiv">
			<label class="layui-form-label required">Agent镜像</label>
			<div class="layui-input-block">
				<input type="text" name="agentImage" placeholder="如：127.0.0.1:20080/skywalking-java-agent:v8.9.0" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
			</div>
		</div>
	</div>
</div>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$,
			parentIndex = layer.index;
		
		form.render();
		
		form.on('select(agentImageSource)', function(data) {
			if(data.value == 1){
				$('#agentVersionDiv').show();
				$('#agentImageDiv').hide();
			}else{
				$('#agentVersionDiv').hide();
				$('#agentImageDiv').show();
			}
		});
		
        form.on('submit(saveBtn)', function(data) {
			if(data.field.agentImageSource == 1){
				if(data.field.agentVersion == ''){
					layer.msg('Agent版本不能为空', {icon: 5, shift: 6});
					return false;
				}
				data.field.agentImage = '';
			}else if(data.field.agentImageSource == 2){
				if(data.field.agentImage == ''){
					layer.msg('Agent镜像不能为空', {icon: 5, shift: 6});
					return false;
				}
				data.field.agentVersion = '';
			}
			$.ajax({
				url: '/globalConfig/traceTemplate/add',
				type: 'POST',
				dataType: "json",
				contentType: 'application/json;charset=UTF-8',
				data: JSON.stringify(data.field),
				success: function(data){
					if(data.code != "000000"){
						layer.msg(data.message, {icon: 5, shift: 6});
						return false;
					}
					layer.close(parentIndex);
					layer.msg("保存成功", {
						icon: 1,
						time: 500,
						shade: 0.01,
						shadeClose: false}, function(){
						parent.layui.table.reload('currentTableId');
					});
				},
				error: function(data){
					layer.close(parentIndex);
					layer.msg(data.message, {icon: 5, shift: 6});
				}
			});
            return false;
        });
    });
</script>